//
//  LCAccountSettingVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/2.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCAccountSettingVC.h"
#import "LCAccountSettingHeader.h"
#import "LCAccountSettingCell.h"

#import "LCUserModel.h"

@interface LCAccountSettingVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSArray *datas;

@property (nonatomic, copy) NSString *registCode;

@end

@implementation LCAccountSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _datas = @[
               @{
                   @"headerTitle":@"账户绑定",
                   @"desTitle":@[@"手机号"]
                 },
               @{
                   @"headerTitle":@"账户安全",
                   @"desTitle":@[@"修改登录密码"]
                }
               ];
    
}

#pragma mark - -
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    NSInteger section = indexPath.section;
    NSDictionary *parameter = _datas[section];
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        LCAccountSettingHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"accountSettingHeader" forIndexPath:indexPath];
        [header lc_updateAccountSettingHeaderWithParmeter:parameter[@"headerTitle"]];
        return header;

    }
    return nil;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;

    LCAccountSettingCell *accountSettingCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"accountSettingCell" forIndexPath:indexPath];
    NSString *phoneNum = @"";
    if (_datas.count > section) {
        phoneNum = (section == 0 && row == 0) ? @"1574732748" : @"";
        [accountSettingCell lc_updateAccountSettingWithParameter:_datas[section] detailDes:phoneNum];
    }
    
    return accountSettingCell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _datas.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSDictionary *dataDic = _datas[section];
    NSArray *desTitles = dataDic[@"desTitle"];
    return desTitles.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SWidth, 40);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SWidth, 50);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger section = indexPath.section;
    switch (section) {
        case 1:  //  重置密码
        {
            
            NSDictionary *sendRegistCode = @{@"phone":[LCUserModel userModelManager].mobile};
            [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/sms" parameter:sendRegistCode responseSuccess:^(id resultCode, id resultParameter) {
                
                
                LCLog(@"resultCode = %@ resultParameter = %@",resultCode,resultParameter);
                NSString *code = [NSString stringWithFormat:@"%@",resultParameter[@"sms_code"]];
                self.registCode = code;
                NSString *idStr = [NSString stringWithFormat:@"%@",resultParameter[@"id"]];
                NSString *token = [NSString stringWithFormat:@"%@",resultParameter[@"sms_token"]];
                
                LCUserModel *userModel = [LCUserModel userModelManager];
                userModel.userid = token;
                LCLog(@"idStr = %@ token = %@",idStr,token);
                //            [self showHint:code];
                
                [self performSegueWithIdentifier:@"resetPassword" sender:self];
                
                
            } responseFail:^(id resultCode, NSError *error) {
                
                [self showHint:@"网络异常,请稍后再试"];
            }];
        }
            break;
            
        default:
            break;
    }
}


- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    if ([segue.identifier isEqualToString:@"resetPassword"]) {
        
        id vc = [segue destinationViewController];
        [vc setValue:[LCUserModel userModelManager].mobile forKey:@"phoneNum"];
        [vc setValue:self.registCode forKey:@"registCode"];
        [vc setValue:@"1" forKey:@"type"];

    }

}


@end
